package com.caregiver.watch.business.care.enums;

import com.caregiver.watch.business.care.model.po.UserPackageOrder;
import lombok.Getter;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Objects;

import static com.caregiver.watch.common.constant.NumberConstants.ZERO;

/**
 * 套餐生效状态枚举
 */
@Getter
public enum PackageEffectivenessStatusEnum {
    
    /**
     * 生效中 - 套餐正在有效期内
     */
    EFFECTIVE(1, "生效中") {

        @Override
        public Integer countGuardDays(UserPackageOrder order) {
            if (Objects.isNull(order.getEffectiveTime())) {
                return ZERO;
            }
            Long until = order.getEffectiveTime().until(LocalDateTime.now(), ChronoUnit.DAYS);
            return until.intValue();
        }
    },
    
    /**
     * 已失效 - 套餐已过期或被取消
     */
    EXPIRED(2, "已失效") {

        @Override
        public Integer countGuardDays(UserPackageOrder order) {
            if (Objects.isNull(order.getEffectiveTime()) || Objects.isNull(order.getExpireTime())) {
                return ZERO;
            }
            Long until = order.getEffectiveTime().until(order.getExpireTime(), ChronoUnit.DAYS);
            return until.intValue();
        }
    },
    
    /**
     * 待生效 - 套餐已购买但尚未到达生效时间
     */
    PENDING(3, "待生效") {

        @Override
        public Integer countGuardDays(UserPackageOrder order) {
            return ZERO;
        }
    };

    private final Integer code;
    private final String description;

    PackageEffectivenessStatusEnum(Integer code, String description) {
        this.code = code;
        this.description = description;
    }


    /**
     * 计算套餐守护天数
     */
    public abstract Integer countGuardDays(UserPackageOrder order);
}
